"use strict";
/* Controllers */
app.controller("smartQueryAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'smartQueryAddService', function ($scope, $location, $rootScope, $routeParams, smartQueryAddService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    vm.p.type = $routeParams.type;
    if (vm.p.type == 'id') {
        vm.p.id = $routeParams.id;
    } else {
        vm.p.queryCode = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.getQueryCmd = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 请求参数
        var params = {
            id: vm.p.id,
            code: vm.p.queryCode
        };

        // 结果处理
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            vm.p = data;
        };
        return smartQueryAddService.getQueryCmd(params, procFn, errorFn);
    };

    $scope.checkQuerySql = function() {
        var vm = $scope.vm;
        alert('success');
    };
    $scope.setQueryCmd = function () {
        var vm = $scope.vm;

        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！");
            } else {
                alert(response.message);
            }
        };
        return smartQueryAddService.setQueryCmd(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageItem: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        if(vm.p.id!='0') {
            $scope.getQueryCmd();
        }
    };

    $scope.initializeController();

}]);


/* Services */
app.service('smartQueryAddService', ['ajaxService', function (ajaxService) {

    this.getQueryCmd = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/getQueryCmd.do", successFunction, errorFunction);
    };

    this.setQueryCmd = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/smartdq/setQueryCmd.do", successFunction, errorFunction);
    };
}]);


